import json
import traceback
from django.http import JsonResponse
from django.views import View
from ...serializers.quantitySerializer import *
from ...config.basic_config import type_conversion_dict
from ...utils.common_util.basic_util import get_model_serializer_config
from ...config.message_config import Config_message_type, Config_message, Error_message_type
from ...utils.related_util.choice_util import get_dimension_choices, get_quantity_type_choices
from utils.encryptionAndDecryptionUtil import get_user_from_useraccount, get_useraccount_from_request
from django.db import IntegrityError, transaction
from utils.responseContentUtil import *


class QuantityConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            query_fields = QuantityListQuerySerializer.get_query_fields()
            single_choice_fields = ["dimension"]
            single_choice_field_dict = {}
            single_choice_field_dict["dimension"] = get_dimension_choices()
            for query_field in query_fields:
                query_field["type"] = type_conversion_dict[query_field["type"]]
                if query_field["dataIndex"] in single_choice_fields:
                    query_field["singleChoice"] = True
                    query_field["choices"] = single_choice_field_dict[query_field["dataIndex"]]
            data["query_fields"] = query_fields
            table_fields = get_model_serializer_config(QuantityListSerializer)
            table_extra_fields = QuantityListSerializer.get_extra_fields_info()
            for extra_field in table_extra_fields:
                for field in table_fields:
                    if field["dataIndex"] == extra_field["dataIndex"]:
                        field["type"] = extra_field["type"]
                        field["title"] = extra_field["title"]
                        break
            table_fields.append(
                {"dataIndex": "operate", "title": "操作", "type": "operate"})
            hidden_fields = ["uuid"]
            sortable_fields = []
            link_fields = ["quantity_name", "available_units"]
            for field in table_fields:
                field["type"] = type_conversion_dict[field["type"]]
                if field["dataIndex"] in hidden_fields:
                    field["hidden"] = True
                if field["dataIndex"] in sortable_fields:
                    field["sortable"] = True
                if field["dataIndex"] in link_fields:
                    field["isLink"] = True
            data["table_fields"] = table_fields
            return make_get_success_response(data=data)

        except Exception as e:
            traceback.print_exc()
            return make_server_error_response(message=str(e), data=None)


class QuantityCreateConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            create_fields = get_model_serializer_config(
                QuantityCreateSerializer)
            single_choice_fields = ["dimension", "quantity_type"]
            single_choice_field_dict = {}
            single_choice_field_dict["dimension"] = get_dimension_choices()
            single_choice_field_dict["quantity_type"] = get_quantity_type_choices(
            )
            for create_field in create_fields:
                create_field["type"] = type_conversion_dict[create_field["type"]]
                if create_field["dataIndex"] in single_choice_fields:
                    create_field["singleChoice"] = True
                    create_field["choices"] = single_choice_field_dict[create_field["dataIndex"]]
            data["create_fields"] = create_fields
            return make_get_success_response(data=data)
        except Exception as e:
            traceback.print_exc()
            return make_server_error_response(message=str(e), data=None)


class QuantityModifyConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            modify_fields = get_model_serializer_config(
                QuantityModifyInfoSerializer)
            single_choice_fields = ["dimension", "quantity_type"]
            single_choice_field_dict = {}
            single_choice_field_dict["dimension"] = get_dimension_choices()
            single_choice_field_dict["quantity_type"] = get_quantity_type_choices(
            )
            for modify_field in modify_fields:
                modify_field["type"] = type_conversion_dict[modify_field["type"]]
                if modify_field["dataIndex"] in single_choice_fields:
                    modify_field["singleChoice"] = True
                    modify_field["choices"] = single_choice_field_dict[modify_field["dataIndex"]]
            data["modify_fields"] = modify_fields
            return make_get_success_response(data=data)
        except Exception as e:
            traceback.print_exc()
            return make_server_error_response(message=str(e), data=None)


class QuantityInfoConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            info_fields = get_model_serializer_config(QuantityInfoSerializer)
            extra_fields = QuantityInfoSerializer.get_extra_fields_info()
            for extra_field in extra_fields:
                for field in info_fields:
                    if field["dataIndex"] == extra_field["dataIndex"]:
                        field["type"] = extra_field["type"]
                        field["title"] = extra_field["title"]
                        break
            for field in info_fields:
                field["type"] = type_conversion_dict[field["type"]]
            data["info_fields"] = info_fields
            return make_get_success_response(data=data)
        except Exception as e:
            traceback.print_exc()
            return make_server_error_response(message=str(e), data=None)


class QuantityUnitConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            modify_filelds = [
                {"title": "单位选择", "dataIndex": "available_units", "type": "CharField"}]
            transfer_fields = ["available_units"]
            transfer_fields_dict = {}
            transfer_fields_dict["available_units"] = []
            for modify_field in modify_filelds:
                modify_field["type"] = type_conversion_dict[modify_field["type"]]
                if modify_field["dataIndex"] in transfer_fields:
                    modify_field["transfer"] = True
                    modify_field["choices"] = transfer_fields_dict[modify_field["dataIndex"]]
            data["modify_fields"] = modify_filelds
            return make_get_success_response(data=data)
        except Exception as e:
            traceback.print_exc()
            return make_server_error_response(message=str(e), data=None)
